require "Common/define"
require "Logic/Config"
require "Common/functions"
---@class DungeonRandomEventManager
DungeonRandomEventManager = DungeonRandomEventManager or {};
local this = DungeonRandomEventManager;

local isNewEvent = false;
local dungeonRandomEventData = nil;
local randomEventShopGoods;
local selectAnswer;
local fightResult;

function DungeonRandomEventManager.init()
    isNewEvent = false;
    selectAnswer = -1;
    randomEventShopGoods = G.dataTable["t_randomeventshopgoods"];
end

function DungeonRandomEventManager.OnDestroy()

end

function DungeonRandomEventManager.OpenDungeonRandomEventUI(callBack)
    isNewEvent = false;
    if logicMgr.GuideMgr.IsGuiding() then
        callBack();
        return;
    end

    if this.hasDungeonRandomEventData() then
        local eventType = dungeonRandomEventData["type"];
        if eventType == 1 then
            -- 购买道具
            showUI(PanelNames.UIRandomEventShop, callBack);
        elseif eventType == 2 then
            -- 租界英雄
            showUI(PanelNames.UIRandomEventHero, callBack);
        elseif eventType == 3 then
            -- 玩家PK
            showUI(PanelNames.UIRandomEventPK, callBack);
        elseif eventType == 4 then
            -- 问答
            showUI(PanelNames.UIRandomEventQuestion, callBack);
        end
    else
        if callBack ~= nil then
            callBack();
        end
    end
end

-- 战斗触发的新事件
function DungeonRandomEventManager.setDungeonRandomEventData(times, msg)
    isNewEvent = true;
    selectAnswer = -1;
    if dungeonRandomEventData == nil then
        dungeonRandomEventData = {};
        --dungeonRandomEventData["useCount"] = 0;
        isNewEvent = false;
    end
    --if times ~= nil then
    --    dungeonRandomEventData["useCount"] = times;
    --end
    if msg == nil or msg.type == nil then
        isNewEvent = false;
    else
        dungeonRandomEventData["type"] = msg.type;
        dungeonRandomEventData["id"] = msg.id or 0;
        dungeonRandomEventData["useTimes"] = msg.useTimes or 0;
        dungeonRandomEventData["overdueTime"] = tonumber(tostring(msg.overdueTime)) * 0.001;

        if msg.shop ~= nil then
            dungeonRandomEventData["shop"] = {};
            for k, v in pairs(msg.shop) do
                local shopItem = {};
                shopItem.commodityId = v.commodityId;
                shopItem.buyTimes = v.buyTimes;
                shopItem.index = v.index;
                shopItem.itemModeId = v.itemModeId;
                table.insert(dungeonRandomEventData["shop"], shopItem);
            end
        else
            dungeonRandomEventData["shop"] = nil;
        end

        if msg.partner ~= nil then
            --dungeonRandomEventData["partner"] = {};
            --local hero = {};
            --local heroconfig = G.dataTable["t_hero"][tostring(msg.partner.modelId)];
            --if heroconfig ~= nil then
            --    logicMgr.HeroManager.copyHeroConfig(heroconfig, hero);
            --    hero["partnerId"] = msg.partner.partnerId;
            --    hero["level"] = msg.partner.lv;
            --    hero["star"] = msg.partner.star;
            --    hero["weaponLevel"] = 1;
            --    hero["power"] = msg.partner.power;
            --    hero["realPower"] = msg.partner.power;
            --    hero["state"] = 0;
            --    hero["leaseHero"] = 1;
            --    hero["useTimes"] = msg.partner.useTimes;
            --    hero["limit"] = msg.partner.limit;
            --    dungeonRandomEventData["partner"] = hero;
            --end
            this.saveDungeonRandomEventHeroData("partner", msg.partner);
        else
            dungeonRandomEventData["partner"] = nil;
        end

        if msg.foePlayerId ~= nil then
            dungeonRandomEventData["foePlayerId"] = {};
            dungeonRandomEventData["foePlayerId"]["playerId"] = tostring(msg.foePlayerId.playerId);
            dungeonRandomEventData["foePlayerId"]["name"] = msg.foePlayerId.name or "";
            dungeonRandomEventData["foePlayerId"]["power"] = tonumber(tostring(msg.foePlayerId.power));
            dungeonRandomEventData["foePlayerId"]["lv"] = msg.foePlayerId.lv;
            dungeonRandomEventData["foePlayerId"]["pic"] = msg.foePlayerId.pic;
            dungeonRandomEventData["foePlayerId"]["picFrame"] = msg.foePlayerId.picFrame;
            dungeonRandomEventData["foePlayerId"]["guildName"] = msg.foePlayerId.guildName or "";
        else
            dungeonRandomEventData["foePlayerId"] = nil;
        end
    end
end

-- 登录推送助力伙伴
function DungeonRandomEventManager.setDungeonRandomEventHeroData(msg)
    if msg ~= nil and msg.partner ~= nil then
        if dungeonRandomEventData == nil then
            dungeonRandomEventData = {};
            isNewEvent = false;
        end
        this.saveDungeonRandomEventHeroData("leasePartner", msg.partner);
        --dungeonRandomEventData["leasePartner"] = {};
        --local hero = {};
        --local heroconfig = G.dataTable["t_hero"][tostring(msg.partner.modelId)];
        --if heroconfig ~= nil then
        --    logicMgr.HeroManager.copyHeroConfig(heroconfig, hero);
        --    hero["partnerId"] = msg.partner.partnerId;
        --    hero["level"] = msg.partner.lv;
        --    hero["star"] = msg.partner.star;
        --    hero["weaponLevel"] = 1;
        --    hero["power"] = msg.partner.power;
        --    hero["realPower"] = msg.partner.power;
        --    hero["state"] = 0;
        --    hero["leaseHero"] = 1;
        --    hero["useTimes"] = msg.partner.useTimes;
        --    hero["limit"] = msg.partner.limit;
        --    dungeonRandomEventData["leasePartner"] = hero;
        --end
        local leasePartner =  dungeonRandomEventData["leasePartner"];
        if leasePartner ~= nil and tonumber(leasePartner.useTimes) >= tonumber(leasePartner.limit) then
            dungeonRandomEventData["leasePartner"] = nil;
        end
    else
        dungeonRandomEventData["leasePartner"] = nil;
    end
end

function DungeonRandomEventManager.saveDungeonRandomEventHeroData(key, partner)
    dungeonRandomEventData[key] = {};
    local hero = {};
    local heroconfig = G.dataTable["t_hero"][tostring(partner.modelId)];
    if heroconfig ~= nil then
        logicMgr.HeroManager.copyHeroConfig(heroconfig, hero);
        hero["partnerId"] = partner.partnerId;
        hero["level"] = partner.lv;
        hero["star"] = partner.star;
        hero["weaponLevel"] = 1;
        hero["power"] = partner.power;
        hero["realPower"] = partner.power;
        hero["state"] = 0;
        hero["leaseHero"] = 1;
        hero["useTimes"] = partner.useTimes or 0;
        hero["limit"] = partner.limit;
        dungeonRandomEventData[key] = hero;
    end
end

-- 设置玩家PK记录
function DungeonRandomEventManager.setDungeonRandomEventFightResult(msg)
    if msg == nil then
        fightResult = nil;
    else
        fightResult = {};
        fightResult.fightId = tostring(msg.fightId);
        fightResult.fightResult = msg.fightResult;
        fightResult.record = msg.record;
        fightResult.items = msg.items or nil;
        logicMgr.FormationManager.SetFormation(EnumConst.FormationType.DungeonEventPK, msg.formation);
    end
end

function DungeonRandomEventManager.ShowDungeonEventPKReward()
    if fightResult ~= nil and fightResult.items ~= nil then
        this.getFetchReward(fightResult.items, function()
            fightResult = nil;
            logicMgr.FlashGiftManager.showNewFlashGift();
        end)
    else
        logicMgr.FlashGiftManager.showNewFlashGift();
    end
end

-- 前端自己维护下数据，强制 +1
function DungeonRandomEventManager.setDungeonRandomEventMaxCount()
    dungeonRandomEventData["useCount"] = tonumber(dungeonRandomEventData["useCount"]) + 1;
end

function DungeonRandomEventManager.getDungeonRandomEventDataEndTime()
    if dungeonRandomEventData == nil or dungeonRandomEventData["type"] == nil then
        return 0;
    end
    return dungeonRandomEventData["overdueTime"];
end

function DungeonRandomEventManager.getDungeonRandomEventData()
    return dungeonRandomEventData;
end

function DungeonRandomEventManager.hasDungeonRandomEventData()
    if dungeonRandomEventData == nil or dungeonRandomEventData["type"] == nil or HelperFunc.GetServerTime() > dungeonRandomEventData["overdueTime"] then
        return false;
    end
    if dungeonRandomEventData["type"] == 1 and dungeonRandomEventData["shop"] == nil then
        return false;
    end
    if dungeonRandomEventData["type"] == 2 and dungeonRandomEventData["partner"] == nil then
        return false;
    end
    if dungeonRandomEventData["type"] == 3 and dungeonRandomEventData["foePlayerId"] == nil then
        return false;
    end
    if dungeonRandomEventData["type"] == 4 and dungeonRandomEventData["id"] == 0 then
        return false;
    end
    -- 判断是否全部购买
    --local maxNum = 10;
    --local globalCfg = G.dataTable["t_global"][tostring(574)];
    --if globalCfg ~= nil then
    --    maxNum = globalCfg.f_int_value;
    --end
    --if dungeonRandomEventData["useCount"] >= maxNum then
    --    return false;
    --end
    --if randomEventShopGoods == nil then
    --    return false;
    --end
    --local cfg = randomEventShopGoods[tostring(dungeonRandomEventData["id"])];
    --if cfg == nil or dungeonRandomEventData["useTimes"] >= cfg.f_Limit then
    --    return false;
    --end
    return true;
end

function DungeonRandomEventManager.getEventState()
    return isNewEvent or false;
end

function DungeonRandomEventManager.setEventState(value)
    isNewEvent = value;
end

--获得奖励
function DungeonRandomEventManager.getFetchReward(data, closeBack)
    logicMgr.HeroManager.showNewHero(function ()
        if data ~= nil then
            local rewards = {};
            for k,v in pairs(data) do
                local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v.itemModelId, v.num);
                if itemData ~= nil then
                    table.insert(rewards, itemData);
                end
            end
            if #rewards > 0 then
                showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards, function()
                    if closeBack ~= nil then
                        closeBack();
                    end
                end);
            end
        end
    end);
end

--设置问答选择
function DungeonRandomEventManager.setSelectAnswer(selectIndex)
    selectAnswer = selectIndex;
end
--获得问答选择
function DungeonRandomEventManager.getSelectAnswer()
    return selectAnswer or -1;
end

function DungeonRandomEventManager.updateLeaseHero(formations)
    if dungeonRandomEventData == nil then
        return;
    end
    local partner = dungeonRandomEventData["leasePartner"];
    if partner == nil then
        return;
    end

    for i = 1, #formations do
        for k = 1, #formations[i] do
            local item = formations[i][k];
            if item ~= nil and tonumber(tostring(item.partnerId)) ~= 0 and tonumber(item.lend) == 1 then
                if partner.partnerId == item.partnerId then
                    partner["useTimes"] = tonumber(partner["useTimes"]) + 1;
                    if partner["useTimes"] >= partner["limit"] then
                        dungeonRandomEventData["leasePartner"] = nil;
                    else
                        dungeonRandomEventData["leasePartner"] = partner;
                    end
                    return;
                end
            end
        end
    end
end

function DungeonRandomEventManager.getLeaseHero()
    if dungeonRandomEventData == nil then
        return nil;
    end
    local partner = dungeonRandomEventData["leasePartner"];
    if partner ~= nil then
        return { partner };
    end
    return nil;
end

return DungeonRandomEventManager